﻿<cfcomponent displayname="Auth" hint="Authenticate" extends="Controller" output="false">

	<cffunction name="init" access="public" hint="init" displayname="init">
		
		<cfscript>
			
			return this;
			
		</cfscript>
		
	</cffunction> 
	
	<cffunction name="login" access="public" hint="login" displayname="login">
		
		<cfif IsUserLoggedIn()>
			
			<cfset redirectTo(controller="Dashboard")>
			
			<cfreturn false>
			
		</cfif>
		
		<cflogin>
		
			<cfset user = model("user").findOne(where="username='#params.username#'")>
			
			<cfif IsObject(user)>
			
				<cfif compare(shaHash(params.password), user.userpassword) eq 0>
					
					<cfif user.isAdmin eq "Y">
						<cfset roles = "admin">
					<cfelse>
						
						<cfset userRole = model("userrole").findAll(where="userid=#user.id#")>
						
						<cfif IsQuery(userRole)>
							<cfset roles = ValueList(userrole.role)>
						<cfelse>
							
							<cfset flashInsert(error="User doesn't have any role. Plese contact your administrator.")>
							<cfset redirectTo(controller="Home")>
							
						</cfif>
						
					</cfif>
					
					<cfloginuser name="#params.username#" password="#params.password#" roles="admin" >
					
					<cfset person = model("person").findOne(where="userid = #user.key()#")>
					
					<cfset SESSION.auth.person = Trim(person.firstName & " " & person.middleName & " " & person.lastName)>
					<cfset SESSION.auth.personalKey = GenerateSecretKey("AES", 128)>
					
					<cfset user.lastLogin = Now()>
					<cfset user.save()>
					
					<!---
					<cfset SESSION.auth.menu = this.createMenu(user.key())>
					--->
				<cfelse>	
					<cfset flashInsert(error="Login failed.")>
					<cfset redirectTo(controller="Home")>
				</cfif> 
			
			<cfelse>
				<cfset flashInsert(error="Login failed.")>
				<cfset redirectTo(controller="Home")>
			</cfif>
				
			<cfset redirectTo(controller="Dashboard")>
		
		</cflogin>
		
	</cffunction> 
	
	<cffunction name="logout" access="public" hint="logout" displayname="logout">
		
		<cflogout>
		
		<cfset StructDelete(SESSION, "auth")>
		
		<cfif StructKeyExists(SESSION, "employee")>
			<cfset StructDelete(SESSION, "employee")>
		</cfif>
		
		<cfset flashInsert(info="Successfully logout.")>
		<cfset redirectTo(controller="Home")>
		
	</cffunction>
	
	<cffunction name="createMenu" access="private" hint="create menu" displayname="create menu">
		<!--- not implemented yet --->
		<cfargument name="userId" hint="user ID" displayname="user ID" required="true" type="numeric">
		
		<cfset local.menuFile = "../lib/menu/menu.cfm">
		<cfset local.hashUserId = md5Hash(arguments.userId & "menu", true) & ".js">
		<cfset local.userFile = "../assets/" & local.hashUserId>
		
		<cfsavecontent variable="local.menu">
			<cfinclude template="#local.menuFile#" >
		</cfsavecontent>
		
		<cfset local.menu = Trim(REReplace(local.menu, "[[:cntrl:]]{2,}", " ", "ALL"))>
		
		<cfif FileExists("../assets/#hashUserId#")>
			
			<cffile action="read" file="#local.userFile#" variable="local.exists" >
			
			<cfif compare(Hash(Trim(local.exists), "MD5"), Hash(local.menu, "MD5")) neq 0>
				<cffile action="write" file="#local.userFile#" output="#local.menu#" >
			</cfif>
			
		<cfelse>
			
			<cffile action="write" file="#local.userFile#" output="#local.menu#" > 
			
		</cfif>
		
		<cfreturn hashUserId>	
		
	</cffunction>
		  
	
</cfcomponent>